package cn.youyeku.zsxh.ui.fragment;

import android.os.Bundle;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.getbase.floatingactionbutton.FloatingActionButton;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.List;

import cn.youyeku.zsxh.R;
import cn.youyeku.zsxh.Utils.LogUtils;
import cn.youyeku.zsxh.adapter.RefreshFootAdapter;
import cn.youyeku.zsxh.api.XhApi;
import cn.youyeku.zsxh.bean.Question_center;
import cn.youyeku.zsxh.ui.UIControl;
import cn.youyeku.zsxh.ui.fragment.ABS.AbsSwipeFragment;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class FgmQuestion extends AbsSwipeFragment implements View.OnClickListener {
    private FloatingActionButton bt_send_shop_post,bt_send_shop_act;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    protected void initView() {
        bt_send_shop_post = (FloatingActionButton) view.findViewById(R.id.fgm_good_exchange_shop_post);
        bt_send_shop_act = (FloatingActionButton) view.findViewById(R.id.fgm_good_exchange_accept);
        bt_send_shop_post.setOnClickListener(this);
        bt_send_shop_act.setOnClickListener(this);
    }

    public FgmQuestion() {
        super(R.layout.fgm_base_refresh_layout, R.id.fgm_base_recycle, R.id.fgm_base_swipe);
    }

    @Override
    protected RefreshFootAdapter getAapter() {
        return new RefreshFootAdapter<Question_center>(R.layout.fgm_goods_exchange_item, R.layout.refresh_footer) {

            @Override
            protected void onBindCustomerData(RefreshViewHolder holder, Question_center data, int pos) {
                ImageView imgHead = holder.getChildView(R.id.good_exchange_imageV_head);
                TextView nick_name = holder.getChildView(R.id.good_exchange_textV_nickname);
                TextView date = holder.getChildView(R.id.good_exchange_textV_date);
                TextView detail = holder.getChildView(R.id.good_exchange_textV_detail_text);
                TextView price = holder.getChildView(R.id.good_exchange_textV_price);
                TextView btn_py = holder.getChildView(R.id.good_exchange_textV_btn_py);
                ImageView pic = holder.getChildView(R.id.good_exchange_imgV_pic);


                price.setText("标题：" + data.getShop_title() );
                date.setText("时间：" + data.getShop_goods_time());
                detail.setText("问题描述：" + data.getShop_content());
                nick_name.setText(data.getNick_name());

                Glide.with(getContext())
                        .load(XhApi.getFace(data.getShop_author_id()))
                        .diskCacheStrategy(DiskCacheStrategy.NONE)
                        .error(R.drawable.default_avatar)
                        .into(imgHead);
                Glide.with(getContext())
                        .load(XhApi.getShop(data.getShop_author_id(), data.getShop_pic()))
                        .diskCacheStrategy(DiskCacheStrategy.NONE)
                        .error(R.drawable.default_avatar)
                        .into(pic);


            }

            @Override
            protected void setmOnItemClick(View itemView, final Question_center data, int pos) {
                itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        UIControl.showActGoodsExchange(getContext(), data);

                    }
                });
            }
        };
    }

    @Override
    protected void requestData() {

        XhApi.get_shops(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String data = response.body().string();
                LogUtils.w("获取回应" + data);
                try {
                    JSONObject object = new JSONObject(data);
                    int code = object.getInt("code");

                    LogUtils.w("code  == " + code + object.getString("message"));

                    if(code ==200){
                        Gson gson = new Gson();
                        mData = gson.fromJson(object.getString("data"), new TypeToken<List<Question_center>>() {
                        }.getType());
                    }
                    Message msg = new Message();
                    msg.what = code;
                    handler.sendMessage(msg);

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.fgm_good_exchange_shop_post:
                UIControl.showActGoodsExchangePost(getContext());
                break;
            case R.id.fgm_good_exchange_accept:
                UIControl.showActGoodsExchangeAccepted(getContext());
                break;

        }
    }
}
